package com.octo.poker.repository;



import java.util.List;


import org.hibernate.Query;
import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.octo.poker.orm.Game;
@Transactional
@Repository
public class GameRepository {
	@Autowired
	protected SessionFactory sessionFactory;
	
	public void save(Game game){
		sessionFactory.getCurrentSession().save(game);
	}
	
	public Long getScore(){
		Query q = sessionFactory.getCurrentSession().createQuery("Select sum(g.result) from Game g");
		return (Long) q.uniqueResult();
	}
	
	public List<Game> getLastFiveGames(){
		Query q = sessionFactory.getCurrentSession().createQuery("Select g from Game g order by g.id DESC");
		q.setMaxResults(5);
		return q.list();
	}
	
	
}
